﻿using CatUtils.Utils.Currency.IoUtils;

using CatUtils.Utils.Helper;
using CatUtils.Utils.SpecialUtils.View;
using RabbitDownload.Helper;
using RabbitDownload.Helper.Currency;

using System;
using System.Collections.Generic;

namespace RabbitDownload.MangerTool
{
    public class FileControlTool
    {
        public string PropagandaPath = StaticValue.BinPath + "Propaganda\\";

        public static void DeleteForSizeOrInfo(String FloderPath, int FileSize, int Width, int Height)
        {
            if (FileSize == 0)
            {
                MessageHelper.Alert("预设值不能为空！");
                return;
            }
            List<string> FilePathList = GetImgFilieList(FloderPath);
            Console.WriteLine("路径{0}获取到总数{1}：", FloderPath, FilePathList.Count);
            int HandleCount = 1;
            foreach (string line in FilePathList)
            {
                Console.WriteLine("总进度{0}%", (float)HandleCount++ / FilePathList.Count * 100);
                double GetFileSize = FileHelper.GetFileSizeForKB(line);
                if (GetFileSize < FileSize)
                {
                    FileHelper.FileDelete(line);
                    Console.WriteLine("文件大小{0}，已删除。", GetFileSize);
                }
                else
                {
                    using (System.Drawing.Image image = System.Drawing.Image.FromFile(line))
                    {
                        if (image.Width < Width && image.Height < Height)
                        {
                            Console.WriteLine(string.Format("文件小于，当前宽高：width:{0},height:{1}", image.Width, image.Height));

                            FileHelper.FileDelete(line);
                        }
                        else
                        {
                            //Console.WriteLine("正常文件" + DateTime.Now.ToString());
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 调整图片大小
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="FileSize"></param>
        public static void ReSize(String FloderPath, int FileSize)
        {
            if (FileSize == 0)
            {
                MessageHelper.Alert("预设值不能为空！");
                return;
            }
            List<string> FilePathList = GetImgFilieList(FloderPath);
            int HandleCount = 1;
            foreach (string FilePath in FilePathList)
            {
                MagickImageHelper.CompressimgToSize(FilePath, FileSize);
                Console.WriteLine("总进度{0}%", (float)HandleCount++ / FilePathList.Count * 100);
            }
        }

        /// <summary>
        /// 获取图像文件的列表
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <returns></returns>
        public static List<string> GetImgFilieList(String FloderPath)
        {
            List<string> FileList = FloderNameHelper.GetFullFileNameList(FloderPath, "*.jpg");
            FileList.AddRange(FloderNameHelper.GetFullFileNameList(FloderPath, "*.png"));
            return FileList;
        }

        /// <summary>
        /// 删除小于X的图片文件
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="FileSize"></param>
        public static void DeleteForSize(string FloderPath, int FileSize)
        {
            if (FileSize == 0)
            {
                MessageHelper.Alert("预设值不能为空！");
                return;
            }
            List<string> FilePathList = GetImgFilieList(FloderPath);
            Console.WriteLine("路径{0}获取到总数{1}：", FloderPath, FilePathList.Count);
            int HandleCount = 1;
            foreach (string line in FilePathList)
            {
                Console.WriteLine("总进度{0}%", (float)HandleCount++ / FilePathList.Count * 100);
                double GetFileSize = FileHelper.GetFileSizeForKB(line);

                if (GetFileSize < FileSize)
                {
                    //   FileHelper.FileDelete(line);
                    Console.WriteLine("文件大小{0}，已删除。", GetFileSize);
                }
            }
        }

        public static void DeleteImgForProportion(String FloderPath, float ProportionCount)
        {
            Console.WriteLine(ProportionCount);
            List<string> FileList = GetImgFilieList(FloderPath);
            Console.WriteLine("获取到{0}个文件", FileList.Count);
            int CompleteCount = 0;

            foreach (string line in FileList)
            {
                Console.WriteLine(line);
                Console.WriteLine("已处理{0}%", (float)CompleteCount++ / FileList.Count * 100);
                System.Drawing.Image image = System.Drawing.Image.FromFile(line);

                float PercentageCount = (float)image.Width / image.Height;
                image.Dispose();
                Console.WriteLine("比例为{0}", PercentageCount);
                if (PercentageCount < ProportionCount)
                {
                    Console.WriteLine("文件已经移动到此目录的子文件（Doubt）下");
                    string childrenFloder = FloderNameHelper.GetFloderPath(line) + "Doubt\\";

                    if (FloderHelper.FloderExits(childrenFloder, true))
                    {
                        Console.WriteLine(childrenFloder + FileNameHelper.GetFileName(line));
                        FileHelper.FileMove(line, childrenFloder + FileNameHelper.GetFileName(line));
                    }
                }
            }
        }

        /// <summary>
        /// 图像压缩辅助
        ///
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="Mode"></param>
        public static void ImgCompress(string FloderPath, string Mode)
        {
            List<string> FileList = GetImgFilieList(FloderPath);
            Console.WriteLine("获取到{0}个文件", FileList.Count);
            int CompleteCount = 0;
            if (Mode == "无损压缩")
            {
                foreach (string line in FileList)
                {
                    MagickImageHelper.CompressImg(line);
                    Console.WriteLine("已处理{0}%", (float)CompleteCount++ / FileList.Count * 100);
                }
            }
            else
            {
                foreach (string line in FileList)
                {
                    Console.WriteLine(line);
                    Console.WriteLine("已处理{0}%", (float)CompleteCount++ / FileList.Count * 100);

                    MagickImageHelper.AutoCompressImg(line, "");
                }
            }
        }

        /// <summary>
        /// 切割文件夹，告知是否添加宣传信息
        /// </summary>
        /// <param name="SplitCount">文件夹切割多少</param>
        /// <param name="Propaganda">是否添加宣传信息</param>
        public static void SplitFloder(string Floder, int SplitCount, bool Propaganda)
        {
            int FloderCount = 1;
            string TempPath = Floder + "\\已切割\\" + FloderNameHelper.GetFloderName(Floder) + "（{0}）\\";

            string MoveToFloder = string.Format(TempPath, FloderCount);
            Console.WriteLine("主缓存文件夹" + TempPath);

            int TempCount = SplitCount;
            List<string> FileList = FloderNameHelper.GetFullFileNameList(Floder);

            while (FileList.Count > 0)
            {
                for (; TempCount > 0; TempCount--)
                {
                    if (FileList.Count <= 0)
                        break;
                    FileHelper.FileCopy(FileList[0], MoveToFloder + FileNameHelper.GetFileName(FileList[0]), true);
                    FileList.RemoveAt(0);
                }

                new FileControlTool().CopyPropaganda(MoveToFloder, Propaganda);
                TempCount = SplitCount;
                MoveToFloder = string.Format(TempPath, ++FloderCount);
            }
        }

        /// <summary>
        /// 拷贝宣传信息
        /// </summary>
        /// <param name="moveToFloder"></param>
        /// <param name="Propaganda"></param>
        public void CopyPropaganda(string moveToFloder, bool Propaganda)
        {
            if (Propaganda)
                foreach (string line in FloderNameHelper.GetFullFileNameList(new FileControlTool().PropagandaPath))
                {
                    FileHelper.FileCopy(line, moveToFloder + FileNameHelper.GetFileName(line), true);
                }
        }
    }
}